"""Simple guided task system for young players."""

from __future__ import annotations

from dataclasses import dataclass
from typing import Callable

from game_state import GameState


Requirement = Callable[[GameState], bool]
Completion = Callable[[GameState], None]


@dataclass(slots=True)
class Task:
    id: str
    title: str
    description: str
    requirement: Requirement
    on_complete: Completion
    next_task_id: str | None = None

    def is_complete(self, state: GameState) -> bool:
        return self.requirement(state)


def _ensure_progress_key(state: GameState, key: str) -> int:
    return state.task_progress.setdefault(key, 0)


class TaskManager:
    def __init__(self, state: GameState) -> None:
        self.state = state
        self.tasks = self._create_tasks()
        self.active_task_id = next(iter(self.tasks), None)
        self.state.current_task = self.active_task_id

    def _create_tasks(self) -> dict[str, Task]:
        return {
            "gather_wood": Task(
                id="gather_wood",
                title="第 1 步: 收集木头",
                description="破坏草地或树木，拾取 3 个木头。",
                requirement=lambda state: state.inventory.get("wood", 0) >= 3,
                on_complete=lambda state: None,
                next_task_id="craft_plank",
            ),
            "craft_plank": Task(
                id="craft_plank",
                title="第 2 步: 合成木板",
                description="使用 C 键合成木板，背包中需要至少 4 个木板。",
                requirement=lambda state: state.inventory.get("plank", 0) >= 4,
                on_complete=lambda state: None,
                next_task_id="build_base",
            ),
            "build_base": Task(
                id="build_base",
                title="第 3 步: 建造小屋",
                description="放置 4 个木板，搭建一个温馨的基地。",
                requirement=lambda state: state.task_progress.get("base_blocks", 0) >= 4,
                on_complete=lambda state: None,
                next_task_id="survive_night",
            ),
            "survive_night": Task(
                id="survive_night",
                title="第 4 步: 度过夜晚",
                description="开启敌人系统并在夜晚坚持到天亮。",
                requirement=lambda state: state.task_progress.get("nights_survived", 0) >= 1,
                on_complete=lambda state: None,
                next_task_id=None,
            ),
        }

    def record_event(self, key: str, amount: int = 1) -> None:
        _ensure_progress_key(self.state, key)
        self.state.task_progress[key] += amount

    def update(self) -> None:
        if not self.active_task_id:
            return
        task = self.tasks[self.active_task_id]
        if task.is_complete(self.state):
            task.on_complete(self.state)
            self.active_task_id = task.next_task_id
            self.state.current_task = self.active_task_id

    def active_task(self) -> Task | None:
        if not self.active_task_id:
            return None
        return self.tasks[self.active_task_id]

    def all_tasks(self) -> list[Task]:
        ordered = []
        current_id = next(iter(self.tasks), None)
        while current_id:
            task = self.tasks[current_id]
            ordered.append(task)
            current_id = task.next_task_id
        return ordered
